﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Net.NetworkInformation;

namespace MultiCastSender
{
    class Program
    {
        public static void listenTcp()
        {
            TcpListener server = new TcpListener(IPAddress.Any,50000);
            server.Start();
            TcpClient client = server.AcceptTcpClient();
            NetworkStream stream = client.GetStream();
            BinaryFormatter formatter = new BinaryFormatter();
            Message msg = null;
            while (true)
            {
                msg = (Message)formatter.Deserialize(stream);
                Console.WriteLine(msg.user + " say : " + msg.text);
            }
        }
        public static String getIp()
        {
            IPHostEntry host;
            string localIP = "";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    //Console.WriteLine(ip.ToString());
                    localIP = ip.ToString();
                }
            }
            return localIP;
        }
        static void Main(string[] args)
        {
            //listen thread

            Thread listenThread = new Thread(new ThreadStart(listenTcp));
            listenThread.IsBackground = true;
            listenThread.Start();

            //send thread //
            String localIP = Program.getIp();           

            /*TcpClient client = new TcpClient(localIP, 50000);
            String strHostName = string.Empty;
            // Getting Ip address of local machine...
            // First get the host name of local machine.
            strHostName = Dns.GetHostName();
            Console.WriteLine("Local Machine's Host Name: " + strHostName);
            // Then using host name, get the IP address list..
            IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
            IPAddress[] addr = ipEntry.AddressList;

            for (int i = 0; i < addr.Length; i++)
            {
                Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
            }*/

            TcpClient client = new TcpClient(localIP, 50000);
            NetworkStream stream = client.GetStream();
            BinaryFormatter formatter = new BinaryFormatter();

            Console.WriteLine("Press any key to send a message");
            Console.WriteLine("Press the key 'q' to quit");

            ConsoleKeyInfo key;
            Message hello = new Message();
            Message quit = new Message("user", "bye");
            do
            {
                key = Console.ReadKey();
                if (key.Key != ConsoleKey.Q)
                {
                    formatter.Serialize(stream, hello);
                }
                else
                {
                    formatter.Serialize(stream, quit);
                }
            } while (key.Key != ConsoleKey.Q);
            stream.Close();
            client.Close();
            

        }
    }
}

